import { RouteRecordNormalized } from 'vue-router'
import cloneDeep from 'lodash/cloneDeep'
import { appRoutes } from '@/router/routes'
interface AsyncRoute {
  code: string
  sort: number
  source: string
  name: string
}
/**获取不显示在menu路由 不走动态路由 */
const getHideRoute = () => {
  const filterItems = items => {
    return items.filter(item => item.meta.hideInMenu)
  }
  const fileHideRoute = routes => {
    return routes
      .map(route => {
        if (!route.children) return null
        const filteredChildren = filterItems(route.children)
        return filteredChildren.length > 0 ? { ...route, children: filteredChildren } : null
      })
      .filter(f => f)
  }
  const hideRoutes: RouteRecordNormalized[] = fileHideRoute(cloneDeep(appRoutes))
  return hideRoutes as RouteRecordNormalized[]
}
//固定的路由 不走动态
const fixRoutes = [...getHideRoute()]
export default data => {
  const extractCodes = route => {
    let codes = [{ code: route.code, sort: route.sort, source: route.source, name: route.name }]
    if (route.children && route.children.length > 0) {
      route.children.forEach(child => {
        codes = codes.concat(extractCodes(child))
      })
    }
    return codes
  }
  /**
   * 提取动态菜单中需要的字段
   */
  let asyncRoutes: AsyncRoute[] = []
  data.forEach(route => {
    asyncRoutes = asyncRoutes.concat(extractCodes(route))
  })

  /**
   * 递归过滤不需要的路由数据
   * @param routes 固定的路由
   * @param asyncRouteList 接口返回的路由
   * @returns
   */
  const filterRoutes = (routes, asyncRouteList = asyncRoutes) => {
    return routes.filter(route => {
      const nameIncluded = asyncRouteList.some(v => v.code == route.name)
      if (nameIncluded && route.children && route.children.length > 0) {
        route.children = filterRoutes(route.children, asyncRouteList)
      }
      if (!nameIncluded) return
      const [_route] = asyncRouteList.filter(v => v.code == route.name)
      /**设置参数 */
      route.meta.order = _route.sort
      route.meta.icon = _route.source || route.meta.icon
      route.meta.locale = _route.name || route.meta.locale
      if (Array.isArray(route.children)) {
        route.children.sort((a, b) => (a.meta.order || 0) - (b.meta.order || 0))
      }
      return nameIncluded || (route.children && route.children.length > 0)
    })
  }

  /**接口响应路由合并固定路由 */
  const mergeArrays = (arr, arr1 = fixRoutes) => {
    const mergedArr: any[] = []
    const nameMap = new Map()

    // 合并arr数组
    arr.forEach(item => {
      if (nameMap.has(item.name)) {
        const existingItem = nameMap.get(item.name)
        existingItem.children = [...new Set([...existingItem.children, ...item.children])]
      } else {
        nameMap.set(item.name, { ...item })
      }
    })

    // 合并arr1数组
    arr1.forEach(item => {
      if (nameMap.has(item.name)) {
        const existingItem = nameMap.get(item.name)
        existingItem.children = [...new Set([...existingItem.children, ...(item.children as any)])]
      } else {
        nameMap.set(item.name, { ...item })
      }
    })

    nameMap.forEach(item => {
      mergedArr.push(item)
    })

    return mergedArr
  }

  return { filterRoutes, mergeArrays }
}
